package com.anmai.crawler1.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.anmai.crawler1.dao.ShopDao;
import com.anmai.crawler1.entity.ShopBean;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class ConvertLocation {
    /**
     * 坐标转换，腾讯地图转换成百度地图坐标
     * @return 返回结果：经度,纬度
     * @param shopBean
     *
     */
    public static ShopBean map_tx2bd(ShopBean shopBean) throws IOException {

        String html = JsoupUtils.getHtml("http://api.map.baidu.com/geoconv/v1/?coords=" + shopBean.getLng() + "," + shopBean.getLat() + "&from=3&to=5&ak=R6gk5NM0fXNpjouptg647WtYnU4F949X");
        JSONObject result = JSON.parseObject(html).getJSONArray("result").getJSONObject(0);
        Double x = result.getDouble("x");
        Double y = result.getDouble("y");

        shopBean.setLng(x);
        shopBean.setLat(y);
        return  shopBean;
    }


    /**
     * 坐标转换，百度地图坐标转换成腾讯地图坐标
     * @param lat  百度坐标纬度
     * @param lng  百度坐标经度
     * @return 返回结果：纬度,经度
     */
    public static String map_bd2tx(double lat, double lng){
        double tx_lat;
        double tx_lon;
        double x_pi=3.14159265358979324;
        double x = lng - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        tx_lon = z * Math.cos(theta);
        tx_lat = z * Math.sin(theta);
        return tx_lat+","+tx_lon;
    }

}
